package com.gaea.tools.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaea.tools.entity.JeCoreResourcetable;
import com.gaea.tools.entity.JeCoreTablecolumn;
import com.gaea.tools.service.IJeCoreResourcetableService;
import com.gaea.tools.service.IJeCoreTablecolumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author erma
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/mdm/je-core-resourcetable")
public class JeCoreResourcetableController {
    @Autowired
    private IJeCoreResourcetableService iJeCoreResourcetableService;
    @Autowired
    private IJeCoreTablecolumnService iJeCoreTablecolumnService;

    @RequestMapping("/refreshSysField")
    public String refreshSysField() {
        List<String> errorTable = new ArrayList<>();
        QueryWrapper<JeCoreResourcetable> jeCoreResourcetableQueryWrapper = new QueryWrapper<JeCoreResourcetable>();
        jeCoreResourcetableQueryWrapper.eq("RESOURCETABLE_ISCREATE", "1");
        jeCoreResourcetableQueryWrapper.notIn("RESOURCETABLE_TYPE", "VIEW", "MODEL");
        List<JeCoreResourcetable> tableList = iJeCoreResourcetableService.list(jeCoreResourcetableQueryWrapper);

        for (JeCoreResourcetable table : tableList) {
            // 修改的字段集合
            Map<String, String> fieldMap = new HashMap<>();

            if (iJeCoreTablecolumnService.list(new QueryWrapper<JeCoreTablecolumn>()
                    .eq("TABLECOLUMN_CODE", "SY_CREATEORG")
                    .eq("TABLECOLUMN_RESOURCETABLE_ID", table.getJeCoreResourcetableId())).size() > 0) {
                fieldMap.put("SY_CREATEORG", "weaver");
                fieldMap.put("SY_CREATEORGNAME", "weaver");
                fieldMap.put("SY_CREATETIME", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                fieldMap.put("SY_CREATEUSER", "weaver");
                fieldMap.put("SY_CREATEUSERNAME", "weaver");
            }

            if (iJeCoreTablecolumnService.list(new QueryWrapper<JeCoreTablecolumn>()
                    .eq("TABLECOLUMN_CODE", "SY_MODIFYORG")
                    .eq("TABLECOLUMN_RESOURCETABLE_ID", table.getJeCoreResourcetableId())).size() > 0) {
                fieldMap.put("SY_MODIFYORG", "weaver");
                fieldMap.put("SY_MODIFYORGNAME", "weaver");
                fieldMap.put("SY_MODIFYTIME", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                fieldMap.put("SY_MODIFYUSER", "weaver");
                fieldMap.put("SY_MODIFYUSERNAME", "weaver");
            }
            String update = "UPDATE " + table.getResourcetableTablecode()
                    + " SET " + table.getResourcetablePkcode() + " = " + table.getResourcetablePkcode();
            for (String key : fieldMap.keySet()) {
                update += "," + key + "= '" + fieldMap.get(key) + "'";
            }
            if (fieldMap.size() > 0) {
                try {
                    iJeCoreResourcetableService.executeUpdate(update);
                } catch (Exception e) {
                    System.out.println("------------------------------------------------" + table.getResourcetableTablecode());
                    errorTable.add(table.getResourcetableTablecode());
                    System.out.println(e.getMessage());
                }
            }

        }


        return JSON.toJSONString(errorTable);
    }
}
